import 'package:baselib/utils/app_info_util.dart';
import 'package:baselib/utils/sp_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:get/get.dart';
import 'package:module_business/comm/route/comm_route.dart';
import 'package:module_business/index/route/index_route.dart';
import 'package:module_business/my/route/my_route.dart';
import 'package:framelib/utils/cupertino_localization_delegate.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SpUtil.getInstance();
  await AppInfoUtil.init();
  runApp(MyApp());
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    statusBarBrightness: Brightness.dark,
    // systemStatusBarContrastEnforced: true
  ));
}

class MyApp extends StatelessWidget {
  MyApp({super.key}) {
    color = createMaterialColor(color_main);
  }

  var color;

  MaterialColor createMaterialColor(Color color) {
    List strengths = <double>[.05];
    Map<int, Color> swatch = <int, Color>{};
    final int r = color.red, g = color.green, b = color.blue;
    for (int i = 1; i < 10; i++) {
      strengths.add(0.1 * i);
    }
    strengths.forEach((strength) {
      final double ds = 0.5 - strength;
      swatch[(strength * 1000).round()] = Color.fromRGBO(
        r + ((ds < 0 ? r : (255 - r)) * ds).round(),
        g + ((ds < 0 ? g : (255 - g)) * ds).round(),
        b + ((ds < 0 ? b : (255 - b)) * ds).round(),
        1,
      );
    });
    return MaterialColor(color.value, swatch);
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
        designSize: const Size(411.42857142857144, 890.2857142857143),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (_, child) {
          return GetMaterialApp(
            title: string_app_name,
            theme: ThemeData(
              useMaterial3: true,

              ///用来适配 Theme.of(context).primaryColorLight 和 primaryColorDark 的颜色变化，不设置可能会是默认蓝色
              primarySwatch: color as MaterialColor,

              /// Card 在 M3 下，会有 apply Overlay
              colorScheme: ColorScheme.fromSeed(
                seedColor: color,
                primary: color,
                brightness: Brightness.light,

                ///影响 card 的表色，因为 M3 下是  applySurfaceTint ，在 Material 里
                surfaceTint: Colors.transparent,
              ),

              /// 受到 iconThemeData.isConcrete 的印象，需要全参数才不会进入 fallback
              iconTheme: IconThemeData(
                size: 24.0,
                fill: 0.0,
                weight: 400.0,
                grade: 0.0,
                opticalSize: 48.0,
                color: Colors.white,
                opacity: 0.8,
              ),

              ///修改 FloatingActionButton的默认主题行为
              floatingActionButtonTheme: FloatingActionButtonThemeData(
                  foregroundColor: Colors.white,
                  backgroundColor: color,
                  shape: CircleBorder()),
              appBarTheme: AppBarTheme(
                iconTheme: IconThemeData(
                  color: Colors.white,
                  size: 24.0,
                ),
                backgroundColor: color_main,
                titleTextStyle: Typography.dense2014.titleLarge,
                systemOverlayStyle: SystemUiOverlayStyle.light,
              ),
            ),
            navigatorKey: Get.key,
            initialRoute: RouteConfig.splashPage,
            // home: ClassicPage(),
            getPages: [
              ...CommRoute.getPages,
              ...MyRoute.getPages,
              ...IndexRoute.getPages,
            ],
            localizationsDelegates: [
              GlobalCupertinoLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              CupertinoLocalizationsDelegate(),
            ],
            supportedLocales: [
              const Locale('en', 'US'), // 美国英语
              const Locale('zh', 'CN'), // 中文简体
            ],
            debugShowCheckedModeBanner: false,
            builder: (context, widget) {
              return MediaQuery(
                ///设置文字大小不随系统设置改变
                data: MediaQuery.of(context)
                    .copyWith(textScaler: TextScaler.noScaling),
                child: widget ??
                    SizedBox(
                      width: 0,
                    ),
              );
            },
          );
        });
  }
}
